<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2025/6/25
  Time: 14:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-3.0.0.min.js"></script>
<script src="js/qrcode.min.js"></script>
<html>
<head>
    <title>Title</title>
</head>
<style>
    html{
       height: 100%;
    }
    body{
        width: 100%;
        height: 100%;
        overflow: hidden;
    }
    #pay-box{
        position: fixed;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(38, 38, 38, 0.7);
        display: none;
        z-index: 9999;
        justify-content: center;
        align-items: center;
    }
    #qr-box{
        margin: 0 auto;
        border: 3px solid #6bc5ff;
    }
</style>
<body>
<div>
    id:<input hidden type="text" name="snackid">
    名称:<input type="text" name="snackname">
    库存:<input type="text" name="stock">
    价格:<input type="text" name="price">
    <div>
        数量:
        <a href="javascript:;" class="numchange">+</a><input value="1" type="text" name="num"><a href="javascript:;" class="numchange">-</a>
    </div>
    总价: <input type="text" class="allprice" name="allprice">
    <button onclick="SnackOrderAddOne()">购买</button>
</div>
 <div id="pay-box">
    <div id="qr-box" style="width: 300px;height: 300px">

    </div>
</div>
</body>
<script>
    var snackIdGet=${param.snackid};
    var userIdGet=1;
    let price=0;//单价
    let stock=0;//库存
    SnackQueryById();
    function SnackQueryById() {
        $.ajax( {
            "url"          :  "SnackQueryByIdServlet",
            "type"         :  "get",
            "data"         :  {"snackid":snackIdGet},
            "dataType" :  "text",
            "beforeSend": function() {
            },
            "success":  function(result) {
                var snack=JSON.parse(result);
                $("input[name='snackid']").val(snack.snackid);
                $("input[name='snackname']").val(snack.snackname);
                $("input[name='stock']").val(snack.stock);
                $("input[name='price']").val(snack.price);
                price=snack.price;
                stock=snack.stock;
                console.log("单价:"+price);
                SnackSumPrice();
            },
            "error":  function() {
            }
        } );
    }
    //=========================================价格计算
    //=========================================价格计算
    //=========================================价格计算
    function SnackSumPrice() {
        let num=$("input[name='num']").val();
        $.ajax( {
            "url"          :  "SnackSumPriceServlet",
            "type"         :  "get",
            "data"         :  {"num":num,"price":price},
            "dataType" :  "text",
            "beforeSend": function() {
            },
            "success":  function(result) {
                let sum=result;
                $(".allprice").val(sum);
            },
            "error":  function() {
            }
        } );
    }
    $(".numchange").click(function () {
        let str=$(this).text();
        IfNum(str);
    });
    $("input[name='num']").change(function () {
        let str=$(this).text();
        IfNum(str);
    })
    function IfNum(str) {
        let numGet=$("input[name='num']").val();
        let num=parseInt(numGet);
        let stocks=parseInt(stock);
        console.log(numGet);
            if(str === '+'){
                num++;
            }else if(str === '-'){
                num--;
            }else {
                console.log("无此按键")
            }
            $("input[name='num']").val(num);
            //最终验证
            if(num < 1 || num > stocks || isNaN(numGet)){
                alert("商品数量选择超出库存且不可小于0或输入的不是数字");
                $("input[name='num']").val(1);
            }
            SnackSumPrice();
    }
    //=========================================订单新增
    //=========================================订单新增
    //=========================================订单新增
    function SnackOrderAddOne() {
        var data={};
        data.snackid=$("input[name='snackid']").val();
        data.uid=userIdGet;
        data.num=$("input[name='num']").val();
        data.allprice=$("input[name='allprice']").val();
        $.ajax( {
            "url"          :  "SnackOrderAddOneServlet",
            "type"         :  "post",
            "data"         :  data,
            "dataType" :  "text",
            "beforeSend": function() {
            },
            "success":  function(result) {
                let results=JSON.parse(result);
                console.log(results);
                if(results.addResult === "success"){
                    $("#pay-box").css("display","flex");
                    var qrcode=new QRCode("qr-box",{
                        text:  results.qrResult,
                        width: 300,
                        height: 300,
                        colorDark: "#292929",
                        colorLight: "#ffffff",
                        correctLevel: QRCode.CorrectLevel.H
                    });
                    ///
                    let snackPayStatus=setInterval(function () {
                        $.ajax( {
                            "url"          :  "PaySnackStatusQueryServlet",
                            "type"         :   "get",
                            "data"         :  {"snackOrderId":results.snackorder.soid,"snackId":results.snackorder.snackid},
                            "dataType" :  "text",
                            "beforeSend": function() {
                            },
                            "success":  function(result) {
                                if(result === "success"){
                                    clearInterval(snackPayStatus);
                                    alert("支付成功");
                                    window.location.href="";
                                }
                            },
                            "error":  function() {
                            }
                        } );
                    },3000);
                }else {
                    alert("订单生成失败")
                }
            },
            "error":  function() {
                alert("网络异常")
            }
        } );
    }
</script>
</html>
